let app = getApp()
const Controller = require('../controllers/controllers.js').controller;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        isHidden: true,
        opacity: 0,
        sortList: [{
            key: '默认排序',
            value: '',
        }, {
            key: '价格从低到高',
            value: 1,
        }, {
            key: '价格从高到低',
            value: 2,
        }, {
            key: '最新上架',
            value: 3,
        }],
        categoryId: '',
        sort: '',
        categoryInfo: {},
        list: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        lastPage: false
    },

    //选择排序
    selectSort(e) {
        let type = e.currentTarget.dataset.type
        this.setData({
            sort: type,
            pageNum: 1,
            show: false,
            list: []
        })
        this.getList()
    },

    //获取分类详情
    getCategoryInfo() {
        Controller.getCategoryinfo({
            categoryId: this.data.categoryId
        }).then(res => {
            this.setData({
                categoryInfo: res.data.baseData
            })
        })
    },

    //获取商品列表
    getList() {
        Controller.goodslist({
            categoryId: this.data.categoryId,
            page: this.data.pageNum,
            pageSize: this.data.pageSize,
            sortBy: this.data.sort
        }).then(res => {
            let list = this.data.list
            list = list.concat(res.data.baseData.list)
            this.setData({
                list,
                lastPage: res.data.baseData.lastPage,
                total: res.data.baseData.totalRow
            })
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.id) {
            this.setData({
                categoryId: options.id
            })
        }
        this.getCategoryInfo()
        this.getList()
    },
    //跳转详情
    goDetail(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/Detail/detail?id=${id}`,
        })
    },
    openSortPopup(e) {
        this.setData({
            show: true
        })
    },
    onClose(e) {
        this.setData({
            show: false,

        })
    },
    handleScroll(e) {

        let scrollTop = e.detail.scrollTop
        if (scrollTop > 50) {
            this.setData({
                isHidden: false
            })
        } else {
            this.setData({
                isHidden: true
            })
        }
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        this.setData({
            topNavigationHeight: app.globalData.topNavigationHeight,
            pxToRpxScale: app.globalData.pxToRpxScale
        })
    },

    onPageScroll(e) {

        let scrollTop = e.scrollTop

        let opacity = scrollTop * this.data.pxToRpxScale / 560

        opacity = opacity > 1 ? 1 : opacity


        this.setData({
            opacity
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.lastPage) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})